home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / RCS_56.ARJ / CI.C < prev    next >
C/C++ Source or Header  |  1992-02-11  |  34KB  |  1,166 lines

  1. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  2.    Copyright 1990, 1991 by Paul Eggert
  3.    Distributed under license by the Free Software Foundation, Inc.
  4.  
  5. This file is part of RCS.
  6.  
  7. RCS is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. RCS is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with RCS; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. Report problems and direct all questions to:
  22.  
  23.     rcs-bugs@cs.purdue.edu
  24.  
  25. */
  26.  
  27. /*
  28.  *                     RCS checkin operation
  29.  */
  30. /*******************************************************************
  31.  *                       check revisions into RCS files
  32.  *******************************************************************
  33.  */
  34.  
  35.  
  36.  
  37. /* $Log: ci.c,v $
  38.  * Revision 5.21  1991/11/20  17:58:07  eggert
  39.  * Don't read the delta tree from a nonexistent RCS file.
  40.  *
  41.  * Revision 5.20  1991/10/07  17:32:46  eggert
  42.  * Fix log bugs.  Remove lint.
  43.  *
  44.  * Revision 5.19  1991/09/26  23:10:30  eggert
  45.  * Plug file descriptor leak.
  46.  *
  47.  * Revision 5.18  1991/09/18  07:29:10  eggert
  48.  * Work around a common ftruncate() bug.
  49.  *
  50.  * Revision 5.17  1991/09/10  22:15:46  eggert
  51.  * Fix test for redirected stdin.
  52.  *
  53.  * Revision 5.16  1991/08/19  23:17:54  eggert
  54.  * When there are no changes, revert to previous revision instead of aborting.
  55.  * Add piece tables, -M, -r$.  Tune.
  56.  *
  57.  * Revision 5.15  1991/04/21  11:58:14  eggert
  58.  * Ensure that working file is newer than RCS file after ci -[lu].
  59.  * Add -x, RCSINIT, MS-DOS support.
  60.  *
  61.  * Revision 5.14  1991/02/28  19:18:47  eggert
  62.  * Don't let a setuid ci create a new RCS file; rcs -i -a must be run first.
  63.  * Fix ci -ko -l mode bug.  Open work file at most once.
  64.  *
  65.  * Revision 5.13  1991/02/25  07:12:33  eggert
  66.  * getdate -> getcurdate (SVR4 name clash)
  67.  *
  68.  * Revision 5.12  1990/12/31  01:00:12  eggert
  69.  * Don't use uninitialized storage when handling -{N,n}.
  70.  *
  71.  * Revision 5.11  1990/12/04  05:18:36  eggert
  72.  * Use -I for prompts and -q for diagnostics.
  73.  *
  74.  * Revision 5.10  1990/11/05  20:30:10  eggert
  75.  * Don't remove working file when aborting due to no changes.
  76.  *
  77.  * Revision 5.9  1990/11/01  05:03:23  eggert
  78.  * Add -I and new -t behavior.  Permit arbitrary data in logs.
  79.  *
  80.  * Revision 5.8  1990/10/04  06:30:09  eggert
  81.  * Accumulate exit status across files.
  82.  *
  83.  * Revision 5.7  1990/09/25  20:11:46  hammer
  84.  * fixed another small typo
  85.  *
  86.  * Revision 5.6  1990/09/24  21:48:50  hammer
  87.  * added cleanups from Paul Eggert.
  88.  *
  89.  * Revision 5.5  1990/09/21  06:16:38  hammer
  90.  * made it handle multiple -{N,n}'s.  Also, made it treat re-directed stdin
  91.  * the same as the terminal
  92.  *
  93.  * Revision 5.4  1990/09/20  02:38:51  eggert
  94.  * ci -k now checks dates more thoroughly.
  95.  *
  96.  * Revision 5.3  1990/09/11  02:41:07  eggert
  97.  * Fix revision bug with `ci -k file1 file2'.
  98.  *
  99.  * Revision 5.2  1990/09/04  08:02:10  eggert
  100.  * Permit adjacent revisions with identical time stamps (possible on fast hosts).
  101.  * Improve incomplete line handling.  Standardize yes-or-no procedure.
  102.  *
  103.  * Revision 5.1  1990/08/29  07:13:44  eggert
  104.  * Expand locker value like co.  Clean old log messages too.
  105.  *
  106.  * Revision 5.0  1990/08/22  08:10:00  eggert
  107.  * Don't require a final newline.
  108.  * Make lock and temp files faster and safer.
  109.  * Remove compile-time limits; use malloc instead.
  110.  * Permit dates past 1999/12/31.  Switch to GMT.
  111.  * Add setuid support.  Don't pass +args to diff.  Check diff's output.
  112.  * Ansify and Posixate.  Add -k, -V.  Remove snooping.  Tune.
  113.  * Check diff's output.
  114.  *
  115.  * Revision 4.9  89/05/01  15:10:54  narten
  116.  * changed copyright header to reflect current distribution rules
  117.  * 
  118.  * Revision 4.8  88/11/08  13:38:23  narten
  119.  * changes from root@seismo.CSS.GOV (Super User)
  120.  * -d with no arguments uses the mod time of the file it is checking in
  121.  * 
  122.  * Revision 4.7  88/08/09  19:12:07  eggert
  123.  * Make sure workfile is a regular file; use its mode if RCSfile doesn't have one.
  124.  * Use execv(), not system(); allow cc -R; remove lint.
  125.  * isatty(fileno(stdin)) -> ttystdin()
  126.  * 
  127.  * Revision 4.6  87/12/18  11:34:41  narten
  128.  * lint cleanups (from Guy Harris)
  129.  * 
  130.  * Revision 4.5  87/10/18  10:18:48  narten
  131.  * Updating version numbers. Changes relative to revision 1.1 are actually
  132.  * relative to 4.3
  133.  * 
  134.  * Revision 1.3  87/09/24  13:57:19  narten
  135.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  136.  * warnings)
  137.  * 
  138.  * Revision 1.2  87/03/27  14:21:33  jenkins
  139.  * Port to suns
  140.  * 
  141.  * Revision 4.3  83/12/15  12:28:54  wft
  142.  * ci -u and ci -l now set mode of working file properly.
  143.  * 
  144.  * Revision 4.2  83/12/05  13:40:54  wft
  145.  * Merged with 3.9.1.1: added calls to clearerr(stdin).
  146.  * made rewriteflag external.
  147.  * 
  148.  * Revision 4.1  83/05/10  17:03:06  wft
  149.  * Added option -d and -w, and updated assingment of date, etc. to new delta.
  150.  * Added handling of default branches.
  151.  * Option -k generates std. log message; fixed undef. pointer in reading of log.
  152.  * Replaced getlock() with findlock(), link--unlink with rename(),
  153.  * getpwuid() with getcaller().
  154.  * Moved all revision number generation to new routine addelta().
  155.  * Removed calls to stat(); now done by pairfilenames().
  156.  * Changed most calls to catchints() with restoreints().
  157.  * Directed all interactive messages to stderr.
  158.  * 
  159.  * Revision 3.9.1.1  83/10/19  04:21:03  lepreau
  160.  * Added clearerr(stdin) to getlogmsg() for re-reading stdin.
  161.  * 
  162.  * Revision 3.9  83/02/15  15:25:44  wft
  163.  * 4.2 prerelease
  164.  * 
  165.  * Revision 3.9  83/02/15  15:25:44  wft
  166.  * Added call to fastcopy() to copy remainder of RCS file.
  167.  *
  168.  * Revision 3.8  83/01/14  15:34:05  wft
  169.  * Added ignoring of interrupts while new RCS file is renamed;
  170.  * Avoids deletion of RCS files by interrupts.
  171.  *
  172.  * Revision 3.7  82/12/10  16:09:20  wft
  173.  * Corrected checking of return code from diff.
  174.  *
  175.  * Revision 3.6  82/12/08  21:34:49  wft
  176.  * Using DATEFORM to prepare date of checked-in revision;
  177.  * Fixed return from addbranch().
  178.  *
  179.  * Revision 3.5  82/12/04  18:32:42  wft
  180.  * Replaced getdelta() with gettree(), SNOOPDIR with SNOOPFILE. Updated
  181.  * field lockedby in removelock(), moved getlogmsg() before calling diff.
  182.  *
  183.  * Revision 3.4  82/12/02  13:27:13  wft
  184.  * added option -k.
  185.  *
  186.  * Revision 3.3  82/11/28  20:53:31  wft
  187.  * Added mustcheckin() to check for redundant checkins.
  188.  * Added xpandfile() to do keyword expansion for -u and -l;
  189.  * -m appends linefeed to log message if necessary.
  190.  * getlogmsg() suppresses prompt if stdin is not a terminal.
  191.  * Replaced keeplock with lockflag, fclose() with ffclose(),
  192.  * %02d with %.2d, getlogin() with getpwuid().
  193.  *
  194.  * Revision 3.2  82/10/18  20:57:23  wft
  195.  * An RCS file inherits its mode during the first ci from the working file,
  196.  * otherwise it stays the same, except that write permission is removed.
  197.  * Fixed ci -l, added ci -u (both do an implicit co after the ci).
  198.  * Fixed call to getlogin(), added call to getfullRCSname(), added check
  199.  * for write error.
  200.  * Changed conflicting identifiers.
  201.  *
  202.  * Revision 3.1  82/10/13  16:04:59  wft
  203.  * fixed type of variables receiving from getc() (char -> int).
  204.  * added include file dbm.h for getting BYTESIZ. This is used
  205.  * to check the return code from diff portably.
  206.  */
  207.  
  208. #include "rcsbase.h"
  209.  
  210. struct Symrev {
  211.        char const *ssymbol;
  212.        int override;
  213.        struct Symrev * nextsym;
  214. };
  215.  
  216. static char const *getcurdate P((void));
  217. static int addbranch P((struct hshentry*,struct buf*));
  218. static int addelta P((void));
  219. static int addsyms P((char const*));
  220. static int fixwork P((mode_t,char const*));
  221. static int removelock P((struct hshentry*));
  222. static int xpandfile P((RILE*,char const*,struct hshentry const*,char const**));
  223. static struct cbuf getlogmsg P((void));
  224. static void cleanup P((void));
  225. static void incnum P((char const*,struct buf*));
  226. static void addassoclst P((int, char *));
  227.  
  228. static FILE *exfile;
  229. static RILE *workptr;            /* working file pointer        */
  230. static struct buf newdelnum;        /* new revision number        */
  231. static struct cbuf msg;
  232. static int exitstatus;
  233. static int forceciflag;            /* forces check in        */
  234. static int keepflag, keepworkingfile, rcsinitflag;
  235. static struct hshentries *gendeltas;    /* deltas to be generated    */
  236. static struct hshentry *targetdelta;    /* old delta to be generated    */
  237. static struct hshentry newdelta;    /* new delta to be inserted    */
  238. static struct stat workstat;
  239. static struct Symrev *assoclst, *lastassoc;
  240.  
  241. mainProg(ciId, "ci", "$Id: ci.c,v 5.21 1991/11/20 17:58:07 eggert Exp $")
  242. {
  243.     static char const cmdusage[] =
  244.         "\nci usage: ci -{fklqru}[rev] -mmsg -{nN}name -sstate -t[textfile] -Vn file ...";
  245.     static char const default_state[] = DEFAULTSTATE;
  246.  
  247.     char altdate[datesize];
  248.     char olddate[datesize];
  249.     char newdatebuf[datesize], targetdatebuf[datesize];
  250.     char *a, **newargv, *textfile;
  251.     char const *author, *krev, *rev, *state;
  252.     char const *diffilename, *expfilename;
  253.     char const *workdiffname, *newworkfilename;
  254.     char const *mtime;
  255.     int lockflag, lockthis, mtimeflag, removedlock;
  256.     int r;
  257.     int changedRCS, changework, newhead;
  258.     int usestatdate; /* Use mod time of file for -d.  */
  259.     mode_t newworkmode; /* mode for working file */
  260.     struct hshentry *workdelta;
  261.     
  262.     setrid();
  263.  
  264.     author = rev = state = textfile = nil;
  265.     lockflag = false;
  266.     mtimeflag = false;
  267.     altdate[0]= '\0'; /* empty alternate date for -d */
  268.     usestatdate=false;
  269.     suffixes = X_DEFAULT;
  270.  
  271.     argc = getRCSINIT(argc, argv, &newargv);
  272.     argv = newargv;
  273.     while (a = *++argv,  0<--argc && *a++=='-') {
  274.         switch (*a++) {
  275.  
  276.                 case 'r':
  277.             keepworkingfile = lockflag = false;
  278.         revno:
  279.             if (*a) {
  280.                 if (rev) warn("redefinition of revision number");
  281.                 rev = a;
  282.                         }
  283.                         break;
  284.  
  285.                 case 'l':
  286.                         keepworkingfile=lockflag=true;
  287.                         goto revno;
  288.  
  289.                 case 'u':
  290.                         keepworkingfile=true; lockflag=false;
  291.                         goto revno;
  292.  
  293.         case 'I':
  294.             interactiveflag = true;
  295.             goto revno;
  296.  
  297.                 case 'q':
  298.                         quietflag=true;
  299.                         goto revno;
  300.  
  301.                 case 'f':
  302.                         forceciflag=true;
  303.                         goto revno;
  304.  
  305.                 case 'k':
  306.                         keepflag=true;
  307.                         goto revno;
  308.  
  309.                 case 'm':
  310.             if (msg.size) redefined('m');
  311.             msg = cleanlogmsg(a, strlen(a));
  312.             if (!msg.size)
  313.                 warn("missing message for -m option");
  314.                         break;
  315.  
  316.                 case 'n':
  317.             if (!*a) {
  318.                                 error("missing symbolic name after -n");
  319.                 break;
  320.                     }
  321.             checksid(a);
  322.             addassoclst(false, a);
  323.                 break;
  324.         
  325.         case 'N':
  326.             if (!*a) {
  327.                                 error("missing symbolic name after -N");
  328.                 break;
  329.                     }
  330.             checksid(a);
  331.             addassoclst(true, a);
  332.                 break;
  333.  
  334.                 case 's':
  335.             if (*a) {
  336.                 if (state) redefined('s');
  337.                 checksid(a);
  338.                 state = a;
  339.             } else
  340.                 warn("missing state for -s option");
  341.                         break;
  342.  
  343.                 case 't':
  344.             if (*a) {
  345.                 if (textfile) redefined('t');
  346.                 textfile = a;
  347.                         }
  348.                         break;
  349.  
  350.         case 'd':
  351.             if (altdate[0] || usestatdate)
  352.                 redefined('d');
  353.             altdate[0] = 0;
  354.             if (!(usestatdate = !*a))
  355.                 str2date(a, altdate);
  356.                         break;
  357.  
  358.         case 'M':
  359.             mtimeflag = true;
  360.             goto revno;
  361.  
  362.         case 'w':
  363.             if (*a) {
  364.                 if (author) redefined('w');
  365.                 checksid(a);
  366.                 author = a;
  367.             } else
  368.                 warn("missing author for -w option");
  369.                         break;
  370.  
  371.         case 'x':
  372.             suffixes = a;
  373.             break;
  374.  
  375.         case 'V':
  376.             setRCSversion(*argv);
  377.             break;
  378.  
  379.  
  380.  
  381.                 default:
  382.             faterror("unknown option: %s%s", *argv, cmdusage);
  383.                 };
  384.         }  /* end processing of options */
  385.  
  386.     if (argc<1) faterror("no input file%s", cmdusage);
  387.  
  388.         /* now handle all filenames */
  389.         do {
  390.         targetdelta=nil;
  391.     ffree();
  392.  
  393.     switch (pairfilenames(argc, argv, rcswriteopen, false, false)) {
  394.  
  395.         case -1:                /* New RCS file */
  396. #        if has_setuid && has_getuid
  397.             if (euid() != ruid()) {
  398.             error("setuid initial checkin prohibited; use `rcs -i -a' first");
  399.             continue;
  400.             }
  401. #        endif
  402.         rcsinitflag = true;
  403.                 break;
  404.  
  405.         case 0:                 /* Error */
  406.                 continue;
  407.  
  408.         case 1:                 /* Normal checkin with prev . RCS file */
  409.         rcsinitflag = !Head;
  410.         }
  411.  
  412.         /* now RCSfilename contains the name of the RCS file, and
  413.          * workfilename contains the name of the working file.
  414.      * If the RCS file exists, finptr contains the file descriptor for the
  415.          * RCS file. The admin node is initialized.
  416.      * RCSstat is set.
  417.          */
  418.  
  419.     diagnose("%s  <--  %s\n", RCSfilename,workfilename);
  420.  
  421.     if (!(workptr = Iopen(workfilename, FOPEN_R_WORK, &workstat))) {
  422.         eerror(workfilename);
  423.         continue;
  424.     }
  425.     if (finptr && !checkaccesslist()) continue; /* give up */
  426.  
  427.     krev = rev;
  428.         if (keepflag) {
  429.                 /* get keyword values from working file */
  430.         if (!getoldkeys(workptr)) continue;
  431.         if (!rev  &&  !*(krev = prevrev.string)) {
  432.             error("can't find a revision number in %s",workfilename);
  433.                         continue;
  434.                 }
  435.         if (!*prevdate.string && *altdate=='\0' && usestatdate==false)
  436.             warn("can't find a date in %s", workfilename);
  437.         if (!*prevauthor.string && !author)
  438.             warn("can't find an author in %s", workfilename);
  439.         if (!*prevstate.string && !state)
  440.             warn("can't find a state in %s", workfilename);
  441.         } /* end processing keepflag */
  442.  
  443.     /* Read the delta tree.  */
  444.     if (finptr)
  445.         gettree();
  446.  
  447.         /* expand symbolic revision number */
  448.     if (!fexpandsym(krev, &newdelnum, workptr))
  449.         continue;
  450.  
  451.         /* splice new delta into tree */
  452.     if ((removedlock = addelta()) < 0)
  453.         continue;
  454.  
  455.     newdelta.num = newdelnum.string;
  456.         newdelta.branches=nil;
  457.         newdelta.lockedby=nil; /*might be changed by addlock() */
  458.     newdelta.selector = true;
  459.     /* set author */
  460.     if (author!=nil)
  461.         newdelta.author=author;     /* set author given by -w         */
  462.     else if (keepflag && *prevauthor.string)
  463.         newdelta.author=prevauthor.string; /* preserve old author if possible*/
  464.     else    newdelta.author=getcaller();/* otherwise use caller's id      */
  465.     newdelta.state = default_state;
  466.     if (state!=nil)
  467.         newdelta.state=state;       /* set state given by -s          */
  468.     else if (keepflag && *prevstate.string)
  469.         newdelta.state=prevstate.string;   /* preserve old state if possible */
  470.     if (usestatdate) {
  471.         time2date(workstat.st_mtime, altdate);
  472.     }
  473.     if (*altdate!='\0')
  474.         newdelta.date=altdate;      /* set date given by -d           */
  475.     else if (keepflag && *prevdate.string) {
  476.         /* Preserve old date if possible.  */
  477.         str2date(prevdate.string, olddate);
  478.         newdelta.date = olddate;
  479.     } else
  480.         newdelta.date = getcurdate();  /* use current date */
  481.     /* now check validity of date -- needed because of -d and -k          */
  482.     if (targetdelta!=nil &&
  483.         cmpnum(newdelta.date,targetdelta->date) < 0) {
  484.         error("Date %s precedes %s in existing revision %s.",
  485.             date2str(newdelta.date, newdatebuf),
  486.             date2str(targetdelta->date, targetdatebuf),
  487.             targetdelta->num
  488.         );
  489.         continue;
  490.     }
  491.  
  492.  
  493.     if (lockflag  &&  addlock(&newdelta) < 0) continue;
  494.     if (!addsyms(newdelta.num))
  495.         continue;
  496.  
  497.     
  498.         putadmin(frewrite);
  499.         puttree(Head,frewrite);
  500.     putdesc(false,textfile);
  501.  
  502.     changework = Expand != OLD_EXPAND;
  503.     lockthis = lockflag;
  504.     workdelta = &newdelta;
  505.  
  506.         /* build rest of file */
  507.     if (rcsinitflag) {
  508.         diagnose("initial revision: %s\n", newdelnum.string);
  509.                 /* get logmessage */
  510.                 newdelta.log=getlogmsg();
  511.         if (!putdftext(newdelnum.string,newdelta.log,workptr,frewrite,false)) continue;
  512.         RCSstat.st_mode = workstat.st_mode;
  513.         changedRCS = true;
  514.         } else {
  515.         diffilename = maketemp(0);
  516.         workdiffname = workfilename;
  517.         if (workdiffname[0] == '+') {
  518.             /* Some diffs have options with leading '+'.  */
  519.             char *dp = ftnalloc(char, strlen(workfilename)+3);
  520.             workdiffname = dp;
  521.             *dp++ = '.';
  522.             *dp++ = SLASH;
  523.             VOID strcpy(dp, workfilename);
  524.         }
  525.         newhead  =  Head == &newdelta;
  526.         if (!newhead)
  527.             foutptr = frewrite;
  528.         expfilename = buildrevision(
  529.             gendeltas, targetdelta, (FILE*)0, false
  530.         );
  531.         if (
  532.             !forceciflag  &&
  533.             (changework = rcsfcmp(
  534.             workptr, &workstat, expfilename, targetdelta
  535.             )) <= 0
  536.         ) {
  537.             diagnose("file is unchanged; reverting to previous revision %s\n",
  538.             targetdelta->num
  539.             );
  540.             if (removedlock < lockflag) {
  541.             diagnose("previous revision was not locked; ignoring -l option\n");
  542.             lockthis = 0;
  543.             }
  544.             if (!(changedRCS  =
  545.                 lockflag < removedlock
  546.             ||  assoclst
  547.             ||    newdelta.state != default_state
  548.                 &&    strcmp(newdelta.state, targetdelta->state) != 0
  549.             ))
  550.             workdelta = targetdelta;
  551.             else {
  552.             /*
  553.              * We have started to build the wrong new RCS file.
  554.              * Start over from the beginning.
  555.              */
  556.             long hwm = ftell(frewrite);
  557.             int bad_truncate;
  558.             if (fseek(frewrite, 0L, SEEK_SET) != 0)
  559.                 Oerror();
  560. #            if !has_ftruncate
  561.                 bad_truncate = 1;
  562. #            else
  563.                 /*
  564.                  * Work around a common ftruncate() bug.
  565.                  * We can't rely on has_truncate, because we might
  566.                  * be using a filesystem exported to us via NFS.
  567.                  */
  568.                 bad_truncate = ftruncate(fileno(frewrite),(off_t)0);
  569.                 if (bad_truncate  &&  errno != EACCES)
  570.                 Oerror();
  571. #            endif
  572.             Irewind(finptr);
  573.             Lexinit();
  574.             getadmin();
  575.             gettree();
  576.             if (!(workdelta = genrevs(
  577.                 targetdelta->num, (char*)0, (char*)0, (char*)0,
  578.                 &gendeltas
  579.             )))
  580.                 continue;
  581.             workdelta->log = targetdelta->log;
  582.             if (newdelta.state != default_state)
  583.                 workdelta->state = newdelta.state;
  584.             if (removedlock && removelock(workdelta)<0)
  585.                 continue;
  586.             if (!addsyms(workdelta->num))
  587.                 continue;
  588.             if (!dorewrite(true, true))
  589.                 continue;
  590.             fastcopy(finptr, frewrite);
  591.             if (bad_truncate)
  592.                 while (ftell(frewrite) < hwm)
  593.                 /* White out any earlier mistake with '\n's.  */
  594.                 /* This is unlikely.  */
  595.                 afputc('\n', frewrite);
  596.             }
  597.         } else {
  598.             diagnose("new revision: %s; previous revision: %s\n",
  599.             newdelnum.string, targetdelta->num
  600.             );
  601.             newdelta.log = getlogmsg();
  602.             switch (run((char*)0, diffilename,
  603.             DIFF DIFF_FLAGS,
  604.             newhead ? workdiffname : expfilename,
  605.             newhead ? expfilename : workdiffname,
  606.             (char*)0
  607.             )) {
  608.             case DIFF_FAILURE: case DIFF_SUCCESS: break;
  609.             default: faterror("diff failed");
  610.             }
  611.             if (newhead) {
  612.             Irewind(workptr);
  613.             if (!putdftext(newdelnum.string,newdelta.log,workptr,frewrite,false)) continue;
  614.             if (!putdtext(targetdelta->num,targetdelta->log,diffilename,frewrite,true)) continue;
  615.             } else
  616.             if (!putdtext(newdelnum.string,newdelta.log,diffilename,frewrite,true)) continue;
  617.             changedRCS = true;
  618.                 }
  619.         }
  620.     if (!donerewrite(changedRCS))
  621.         continue;
  622.  
  623.         if (!keepworkingfile) {
  624.         Izclose(&workptr);
  625.         r = un_link(workfilename); /* Get rid of old file */
  626.         } else {
  627.         newworkmode = WORKMODE(RCSstat.st_mode,
  628.             !   (Expand==VAL_EXPAND  ||  lockthis < StrictLocks)
  629.         );
  630.         mtime = mtimeflag ? workdelta->date : (char const*)0;
  631.  
  632.         /* Expand if it might change or if we can't fix mode, time.  */
  633.         if (changework  ||  (r=fixwork(newworkmode,mtime)) != 0) {
  634.             Irewind(workptr);
  635.             /* Expand keywords in file.  */
  636.             locker_expansion = lockthis;
  637.             switch (xpandfile(
  638.             workptr, workfilename,
  639.             workdelta, &newworkfilename
  640.             )) {
  641.             default:
  642.                 continue;
  643.  
  644.             case 0:
  645.                 /*
  646.                  * No expansion occurred; try to reuse working file
  647.                  * unless we already tried and failed.
  648.                  */
  649.                 if (changework)
  650.                 if ((r=fixwork(newworkmode,mtime)) == 0)
  651.                     break;
  652.                 /* fall into */
  653.             case 1:
  654.                 if (!(r = setfiledate(newworkfilename,mtime))) {
  655.                 Izclose(&workptr);
  656.                 ignoreints();
  657.                 r = chnamemod(&exfile, newworkfilename, workfilename, newworkmode);
  658.                 keepdirtemp(newworkfilename);
  659.                 restoreints();
  660.                 }
  661.             }
  662.         }
  663.         }
  664.     if (r != 0) {
  665.         eerror(workfilename);
  666.         continue;
  667.     }
  668.     diagnose("done\n");
  669.  
  670.         } while (cleanup(),
  671.                  ++argv, --argc >=1);
  672.  
  673.     tempunlink();
  674.     exitmain(exitstatus);
  675. }       /* end of main (ci) */
  676.  
  677.     static void
  678. cleanup()
  679. {
  680.     if (nerror) exitstatus = EXIT_FAILURE;
  681.     Izclose(&finptr);
  682.     Izclose(&workptr);
  683.     Ozclose(&exfile);
  684.     Ozclose(&fcopy);
  685.     Ozclose(&frewrite);
  686.     dirtempunlink();
  687. }
  688.  
  689. #if lint
  690. #    define exiterr ciExit
  691. #endif
  692.     exiting void
  693. exiterr()
  694. {
  695.     dirtempunlink();
  696.     tempunlink();
  697.     _exit(EXIT_FAILURE);
  698. }
  699.  
  700. /*****************************************************************/
  701. /* the rest are auxiliary routines                               */
  702.  
  703.  
  704.     static int
  705. addelta()
  706. /* Function: Appends a delta to the delta tree, whose number is
  707.  * given by newdelnum.  Updates Head, newdelnum, newdelnumlength,
  708.  * and the links in newdelta.
  709.  * Return -1 on error, 1 if a lock is removed, 0 otherwise.
  710.  */
  711. {
  712.     register char *tp;
  713.     register unsigned i;
  714.     int removedlock;
  715.     unsigned newdnumlength;  /* actual length of new rev. num. */
  716.  
  717.     newdnumlength = countnumflds(newdelnum.string);
  718.  
  719.     if (rcsinitflag) {
  720.                 /* this covers non-existing RCS file and a file initialized with rcs -i */
  721.         if ((newdnumlength==0)&&(Dbranch!=nil)) {
  722.             bufscpy(&newdelnum, Dbranch);
  723.             newdnumlength = countnumflds(Dbranch);
  724.         }
  725.         if (newdnumlength==0) bufscpy(&newdelnum, "1.1");
  726.         else if (newdnumlength==1) bufscat(&newdelnum, ".1");
  727.         else if (newdnumlength>2) {
  728.             error("Branch point doesn't exist for %s.",newdelnum.string);
  729.             return -1;
  730.                 } /* newdnumlength == 2 is OK;  */
  731.                 Head = &newdelta;
  732.                 newdelta.next=nil;
  733.         return 0;
  734.         }
  735.         if (newdnumlength==0) {
  736.                 /* derive new revision number from locks */
  737.         switch (findlock(true, &targetdelta)) {
  738.  
  739.           default:
  740.             /* found two or more old locks */
  741.             return -1;
  742.  
  743.           case 1:
  744.                     /* found an old lock */
  745.                     /* check whether locked revision exists */
  746.             if (!genrevs(targetdelta->num,(char*)0,(char*)0,(char*)0,&gendeltas))
  747.             return -1;
  748.                     if (targetdelta==Head) {
  749.                         /* make new head */
  750.                         newdelta.next=Head;
  751.                         Head= &newdelta;
  752.             } else if (!targetdelta->next && countnumflds(targetdelta->num)>2) {
  753.                         /* new tip revision on side branch */
  754.                         targetdelta->next= &newdelta;
  755.                         newdelta.next = nil;
  756.                     } else {
  757.                         /* middle revision; start a new branch */
  758.             bufscpy(&newdelnum, "");
  759.             return addbranch(targetdelta,&newdelnum);
  760.                     }
  761.             incnum(targetdelta->num, &newdelnum);
  762.             return 1; /* successful use of existing lock */
  763.  
  764.           case 0:
  765.                     /* no existing lock; try Dbranch */
  766.                     /* update newdelnum */
  767.             if (StrictLocks || !myself(RCSstat.st_uid)) {
  768.             error("no lock set by %s",getcaller());
  769.             return -1;
  770.                     }
  771.                     if (Dbranch) {
  772.             bufscpy(&newdelnum, Dbranch);
  773.                     } else {
  774.             incnum(Head->num, &newdelnum);
  775.                     }
  776.             newdnumlength = countnumflds(newdelnum.string);
  777.                     /* now fall into next statement */
  778.                 }
  779.         }
  780.         if (newdnumlength<=2) {
  781.                 /* add new head per given number */
  782.                 if(newdnumlength==1) {
  783.                     /* make a two-field number out of it*/
  784.             if (cmpnumfld(newdelnum.string,Head->num,1)==0)
  785.             incnum(Head->num, &newdelnum);
  786.             else
  787.             bufscat(&newdelnum, ".1");
  788.                 }
  789.         if (cmpnum(newdelnum.string,Head->num) <= 0) {
  790.                     error("deltanumber %s too low; must be higher than %s",
  791.               newdelnum.string, Head->num);
  792.             return -1;
  793.                 }
  794.         targetdelta = Head;
  795.         if (0 <= (removedlock = removelock(Head))) {
  796.             if (!genrevs(Head->num,(char*)0,(char*)0,(char*)0,&gendeltas))
  797.             return -1;
  798.             newdelta.next = Head;
  799.             Head = &newdelta;
  800.         }
  801.         return removedlock;
  802.         } else {
  803.                 /* put new revision on side branch */
  804.                 /*first, get branch point */
  805.         tp = newdelnum.string;
  806.         for (i = newdnumlength - (newdnumlength&1 ^ 1);  (--i);  )
  807.             while (*tp++ != '.')
  808.                 ;
  809.         *--tp = 0; /* Kill final dot to get old delta temporarily. */
  810.         if (!(targetdelta=genrevs(newdelnum.string,(char*)nil,(char*)nil,(char*)nil,&gendeltas)))
  811.             return -1;
  812.         if (cmpnum(targetdelta->num, newdelnum.string) != 0) {
  813.             error("can't find branchpoint %s", newdelnum.string);
  814.             return -1;
  815.                 }
  816.         *tp = '.'; /* Restore final dot. */
  817.         return addbranch(targetdelta,&newdelnum);
  818.         }
  819. }
  820.  
  821.  
  822.  
  823.     static int
  824. addbranch(branchpoint,num)
  825.     struct hshentry *branchpoint;
  826.     struct buf *num;
  827. /* adds a new branch and branch delta at branchpoint.
  828.  * If num is the null string, appends the new branch, incrementing
  829.  * the highest branch number (initially 1), and setting the level number to 1.
  830.  * the new delta and branchhead are in globals newdelta and newbranch, resp.
  831.  * the new number is placed into num.
  832.  * Return -1 on error, 1 if a lock is removed, 0 otherwise.
  833.  */
  834. {
  835.     struct branchhead *bhead, **btrail;
  836.     struct buf branchnum;
  837.     int removedlock, result;
  838.     unsigned field, numlength;
  839.     static struct branchhead newbranch;  /* new branch to be inserted */
  840.  
  841.     numlength = countnumflds(num->string);
  842.  
  843.         if (branchpoint->branches==nil) {
  844.                 /* start first branch */
  845.                 branchpoint->branches = &newbranch;
  846.                 if (numlength==0) {
  847.             bufscpy(num, branchpoint->num);
  848.             bufscat(num, ".1.1");
  849.         } else if (numlength&1)
  850.             bufscat(num, ".1");
  851.                 newbranch.nextbranch=nil;
  852.  
  853.     } else if (numlength==0) {
  854.                 /* append new branch to the end */
  855.                 bhead=branchpoint->branches;
  856.                 while (bhead->nextbranch) bhead=bhead->nextbranch;
  857.                 bhead->nextbranch = &newbranch;
  858.         bufautobegin(&branchnum);
  859.         getbranchno(bhead->hsh->num, &branchnum);
  860.         incnum(branchnum.string, num);
  861.         bufautoend(&branchnum);
  862.         bufscat(num, ".1");
  863.                 newbranch.nextbranch=nil;
  864.         } else {
  865.                 /* place the branch properly */
  866.         field = numlength - (numlength&1 ^ 1);
  867.                 /* field of branch number */
  868.         btrail = &branchpoint->branches;
  869.         while (0 < (result=cmpnumfld(num->string,(*btrail)->hsh->num,field))) {
  870.             btrail = &(*btrail)->nextbranch;
  871.             if (!*btrail) {
  872.                 result = -1;
  873.                 break;
  874.             }
  875.                 }
  876.         if (result < 0) {
  877.                         /* insert/append new branchhead */
  878.             newbranch.nextbranch = *btrail;
  879.             *btrail = &newbranch;
  880.             if (numlength&1) bufscat(num, ".1");
  881.                 } else {
  882.                         /* branch exists; append to end */
  883.             bufautobegin(&branchnum);
  884.             getbranchno(num->string, &branchnum);
  885.             targetdelta=genrevs(branchnum.string,(char*)nil,
  886.                         (char*)nil,(char*)nil,&gendeltas);
  887.             bufautoend(&branchnum);
  888.             if (!targetdelta)
  889.                 return -1;
  890.             if (cmpnum(num->string,targetdelta->num) <= 0) {
  891.                                 error("deltanumber %s too low; must be higher than %s",
  892.                       num->string,targetdelta->num);
  893.                 return -1;
  894.                         }
  895.             if (0 <= (removedlock = removelock(targetdelta))) {
  896.                 if (numlength&1)
  897.                 incnum(targetdelta->num,num);
  898.                 targetdelta->next = &newdelta;
  899.                 newdelta.next = 0;
  900.             }
  901.             return removedlock;
  902.             /* Don't do anything to newbranch.  */
  903.                 }
  904.         }
  905.         newbranch.hsh = &newdelta;
  906.         newdelta.next=nil;
  907.     return 0;
  908. }
  909.  
  910.     static int
  911. addsyms(num)
  912.     char const *num;
  913. {
  914.     register struct Symrev *p;
  915.  
  916.     for (p = assoclst;  p;  p = p->nextsym)
  917.         if (!addsymbol(num, p->ssymbol, p->override))
  918.             return false;
  919.     return true;
  920. }
  921.  
  922.  
  923.     static void
  924. incnum(onum,nnum)
  925.     char const *onum;
  926.     struct buf *nnum;
  927. /* Increment the last field of revision number onum by one and
  928.  * place the result into nnum.
  929.  */
  930. {
  931.     register char *tp, *np;
  932.     register size_t l;
  933.  
  934.     l = strlen(onum);
  935.     bufalloc(nnum, l+2);
  936.     np = tp = nnum->string;
  937.     VOID strcpy(np, onum);
  938.     for (tp = np + l;  np != tp;  )
  939.         if (isdigit(*--tp)) {
  940.             if (*tp != '9') {
  941.                 ++*tp;
  942.                 return;
  943.             }
  944.             *tp = '0';
  945.         } else {
  946.             tp++;
  947.             break;
  948.         }
  949.     /* We changed 999 to 000; now change it to 1000.  */
  950.     *tp = '1';
  951.     tp = np + l;
  952.     *tp++ = '0';
  953.     *tp = 0;
  954. }
  955.  
  956.  
  957.  
  958.     static int
  959. removelock(delta)
  960. struct hshentry * delta;
  961. /* function: Finds the lock held by caller on delta,
  962.  * removes it, and returns nonzero if successful.
  963.  * Print an error message and return -1 if there is no such lock.
  964.  * An exception is if !StrictLocks, and caller is the owner of
  965.  * the RCS file. If caller does not have a lock in this case,
  966.  * return 0; return 1 if a lock is actually removed.
  967.  */
  968. {
  969.     register struct lock *next, **trail;
  970.     char const *num;
  971.  
  972.         num=delta->num;
  973.     for (trail = &Locks;  (next = *trail);  trail = &next->nextlock)
  974.         if (next->delta == delta)
  975.         if (strcmp(getcaller(), next->login) == 0) {
  976.             /* We found a lock on delta by caller; delete it.  */
  977.             *trail = next->nextlock;
  978.             delta->lockedby = 0;
  979.             return 1;
  980.         } else {
  981.                     error("revision %s locked by %s",num,next->login);
  982.             return -1;
  983.                 }
  984.     if (!StrictLocks && myself(RCSstat.st_uid))
  985.         return 0;
  986.     error("no lock set by %s for revision %s", getcaller(), num);
  987.     return -1;
  988. }
  989.  
  990.  
  991.  
  992.     static char const *
  993. getcurdate()
  994. /* Return a pointer to the current date.  */
  995. {
  996.     static char buffer[datesize]; /* date buffer */
  997.     time_t t;
  998.  
  999.     if (!buffer[0]) {
  1000.         t = time((time_t *)0);
  1001.         if (t == -1)
  1002.             faterror("time not available");
  1003.         time2date(t, buffer);
  1004.     }
  1005.         return buffer;
  1006. }
  1007.  
  1008.     static int
  1009. #if has_prototypes
  1010. fixwork(mode_t newworkmode, char const *mtime)
  1011.   /* The `#if has_prototypes' is needed because mode_t might promote to int.  */
  1012. #else
  1013.   fixwork(newworkmode, mtime)
  1014.     mode_t newworkmode;
  1015.     char const *mtime;
  1016. #endif
  1017. {
  1018.     int r;
  1019.     return
  1020.             1 < workstat.st_nlink
  1021.             ||    newworkmode&S_IWUSR && !myself(workstat.st_uid)
  1022.         ?   -1
  1023.         :
  1024.             workstat.st_mode != newworkmode
  1025.             &&
  1026.             (r =
  1027. #                if has_fchmod
  1028.                 fchmod(Ifileno(workptr), newworkmode)
  1029. #                else
  1030.                 chmod(workfilename, newworkmode)
  1031. #                endif
  1032.             ) != 0
  1033.         ?   r
  1034.         :
  1035.         setfiledate(workfilename, mtime);
  1036. }
  1037.  
  1038.     static int
  1039. xpandfile(unexfile, dir, delta, exfilename)
  1040.     RILE *unexfile;
  1041.     char const *dir;
  1042.     struct hshentry const *delta;
  1043.     char const **exfilename;
  1044. /*
  1045.  * Read unexfile and copy it to a
  1046.  * file in dir, performing keyword substitution with data from delta.
  1047.  * Return -1 if unsuccessful, 1 if expansion occurred, 0 otherwise.
  1048.  * If successful, stores the stream descriptor into *EXFILEP
  1049.  * and its name into *EXFILENAME.
  1050.  */
  1051. {
  1052.     char const *targetfname;
  1053.     int e, r;
  1054.  
  1055.     targetfname = makedirtemp(dir, 1);
  1056.     if (!(exfile = fopen(targetfname, FOPEN_W_WORK))) {
  1057.         eerror(targetfname);
  1058.         error("can't expand working file");
  1059.         return -1;
  1060.         }
  1061.     r = 0;
  1062.     if (Expand == OLD_EXPAND)
  1063.         fastcopy(unexfile,exfile);
  1064.     else {
  1065.         for (;;) {
  1066.             e = expandline(unexfile,exfile,delta,false,(FILE*)nil);
  1067.             if (e < 0)
  1068.                 break;
  1069.             r |= e;
  1070.             if (e <= 1)
  1071.                 break;
  1072.         }
  1073.     }
  1074.     *exfilename = targetfname;
  1075.     aflush(exfile);
  1076.     return r & 1;
  1077. }
  1078.  
  1079.  
  1080.  
  1081.  
  1082. /* --------------------- G E T L O G M S G --------------------------------*/
  1083.  
  1084.  
  1085.     static struct cbuf
  1086. getlogmsg()
  1087. /* Obtain and yield a log message.
  1088.  * If a log message is given with -m, yield that message.
  1089.  * If this is the initial revision, yield a standard log message.
  1090.  * Otherwise, reads a character string from the terminal.
  1091.  * Stops after reading EOF or a single '.' on a
  1092.  * line. getlogmsg prompts the first time it is called for the
  1093.  * log message; during all later calls it asks whether the previous
  1094.  * log message can be reused.
  1095.  */
  1096. {
  1097.     static char const
  1098.         emptych[] = EMPTYLOG,
  1099.         initialch[] = "Initial revision";
  1100.     static struct cbuf const
  1101.         emptylog = { emptych, sizeof(emptych)-sizeof(char) },
  1102.         initiallog = { initialch, sizeof(initialch)-sizeof(char) };
  1103.     static struct buf logbuf;
  1104.     static struct cbuf logmsg;
  1105.  
  1106.     register char *tp;
  1107.     register size_t i;
  1108.     char const *caller;
  1109.  
  1110.     if (msg.size) return msg;
  1111.  
  1112.     if (keepflag) {
  1113.         /* generate std. log message */
  1114.         caller = getcaller();
  1115.         i = sizeof(ciklog)+strlen(caller)+3;
  1116.         bufalloc(&logbuf, i+datesize);
  1117.         tp = logbuf.string;
  1118.         VOID sprintf(tp, "%s%s at ", ciklog, caller);
  1119.         VOID date2str(getcurdate(), tp+i);
  1120.         logmsg.string = tp;
  1121.         logmsg.size = strlen(tp);
  1122.         return logmsg;
  1123.     }
  1124.  
  1125.     if (!targetdelta && (
  1126.         cmpnum(newdelnum.string,"1.1")==0 ||
  1127.         cmpnum(newdelnum.string,"1.0")==0
  1128.     ))
  1129.         return initiallog;
  1130.  
  1131.     if (logmsg.size) {
  1132.                 /*previous log available*/
  1133.         if (yesorno(true, "reuse log message of previous file? [yn](y): "))
  1134.         return logmsg;
  1135.         }
  1136.  
  1137.         /* now read string from stdin */
  1138.     logmsg = getsstdin("m", "log message", "", &logbuf);
  1139.  
  1140.         /* now check whether the log message is not empty */
  1141.     if (logmsg.size)
  1142.         return logmsg;
  1143.     return emptylog;
  1144. }
  1145.  
  1146. /*  Make a linked list of Symbolic names  */
  1147.  
  1148.         static void
  1149. addassoclst(flag, sp)
  1150. int  flag;
  1151. char * sp;
  1152. {
  1153.         struct Symrev *pt;
  1154.     
  1155.     pt = talloc(struct Symrev);
  1156.     pt->ssymbol = sp;
  1157.     pt->override = flag;
  1158.     pt->nextsym = nil;
  1159.     if (lastassoc)
  1160.             lastassoc->nextsym = pt;
  1161.     else
  1162.             assoclst = pt;
  1163.     lastassoc = pt;
  1164.     return;
  1165. }
  1166.